package sns.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import sns.bean.Impression;
import sns.bean.Relation;
import sns.bean.User;

public class ImpressionDao extends HibernateDaoSupport {

	public void save(Impression impression){
		getSession().save(impression);
	}
	
	public void merge(Impression impression){
		getSession().merge(impression);
	}
	
	public void delete(Impression impression){
		getSession().delete(impression);
	}
	
	/**
	 * get impression by its id
	 * 
	 * @param idimpression
	 * @return
	 */
	public Impression getImpressionById(Long idimpression){
		if(idimpression==null){
			return null;
		}
		Query q = getSession().createQuery("select impression from Impression as impression where impression.idimpression = :idimpression");
		q.setLong("idimpression", idimpression);
		return (Impression)q.uniqueResult();
	}
	
	/**
	 * get impressions of the user
	 * 
	 * @param user
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Impression> getImpressionsOfUser(User user){
		if(user==null){
			return null;
		}
		Query q = getSession().createQuery("select impression from Impression as impression where impression.relation.target.iduser = :iduser");
		q.setLong("iduser", user.getIduser());
		return (List<Impression>)q.list();
	}
	
	/**
	 * get the impressions by relation
	 * @param relation
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Impression> getImpressionsByRelation(Relation relation){
		if(relation==null){
			return null;
		}
		Query q = getSession().createQuery("select impression from Impression as impression where impression.relation.idrelation = :idrelation");
		q.setLong("idrelation", relation.getIdrelation());
		return (List<Impression>)q.list();
	}
}
